﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GraphicLib;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace GraphicIdentification
{
    public partial class frmSetting : Form
    {
        public frmSetting()
        {
            InitializeComponent();
            //读取当前产品的参数信息
            DeserializeParameters();
            input_splitcount.Text = ProductCheck.SPLIT_COUNT.ToString();
            input_max_splitcount.Text = ProductCheck.SPLIT_MAX_COUNT.ToString();
            input_tolerance.Text = ProductCheck.VARIANCE_TOLERANCE.ToString();
        }

        private void btn_parm_save_Click(object sender, EventArgs e)
        {
            //如果切割次数有改变，则重新切割后开始一个新检测进程
            if (Sample.SELECTED_PRODUCT == "" || Sample.SELECTED_PRODUCT==null)
            {
                MessageBox.Show("请先选定一个产品!");
            }
            else
            {
                if (ProductCheck.SPLIT_COUNT != Convert.ToInt32(input_splitcount.Text))
                {
                    ProcessThread.KillAllThreads();
                    ProductCheck.SPLIT_COUNT = Convert.ToInt32(input_splitcount.Text);
                    ProductCheck.VARIANCE_TOLERANCE = Convert.ToDouble(input_tolerance.Text);
                    SamplesInition.ReCutInit();
                }
                else
                {
                    ProductCheck.SPLIT_COUNT = Convert.ToInt32(input_splitcount.Text);
                    ProductCheck.VARIANCE_TOLERANCE = Convert.ToDouble(input_tolerance.Text);
                }
                SerializeParameters();
                MessageBox.Show("参数保存成功！");
            }
            this.Close();
        }

        /// <summary>
        /// 序列化当前的样本数据
        /// </summary>
        public static void SerializeParameters()
        {
            using (FileStream fs = new FileStream(Sample.PARAMETERS_DATA_PATH, FileMode.Create))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                Dictionary<string, string> parameters = new Dictionary<string, string>();
                parameters.Add("SPLIT_COUNT", ProductCheck.SPLIT_COUNT.ToString());
                parameters.Add("VARIANCE_TOLERANCE",ProductCheck.VARIANCE_TOLERANCE.ToString());
                formatter.Serialize(fs, parameters);
            }
        }

        /// <summary>
        /// 反序列化已存在的样本数据
        /// </summary>
        public static void DeserializeParameters()
        {
            if (File.Exists(Sample.PARAMETERS_DATA_PATH))
            {
                using (FileStream fs = new FileStream(Sample.PARAMETERS_DATA_PATH, FileMode.Open))
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    Dictionary<string, string> parameters = new Dictionary<string, string>();
                    parameters=(Dictionary<string, string>)formatter.Deserialize(fs);
                    ProductCheck.SPLIT_COUNT = Convert.ToInt32(parameters["SPLIT_COUNT"]);
                    ProductCheck.VARIANCE_TOLERANCE = Convert.ToDouble(parameters["VARIANCE_TOLERANCE"]);
                }
            }
        }

    }
}
